<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');


/**
 * YekComputer (r) CMS controller class
 * 
 * @version: 1.0
 * @copyright (c) 2009, YekComputer. All rights reserved.
 * @author Babak Taraghi 'babak.taraghi@gmail.com'
 * @license Code licensed under the BSD License.
 */
class Pages extends CI_Controller {
	
	
	/**
	 * @desc Array - datas which should pass to the views
	 */
	var $data = array();
	
	
	/**
	 * @desc Controller Constructor
	 */
	public function Pages(){
		parent::__construct();
		$this->load->model("categoriesModel");
		$this->load->model("pagesModel");
		
		$this->load->helper("form");
		
		$categories = $this->categoriesModel->getAllChildren($this->categoriesModel->getRecordByInternalName("products"));
		foreach($categories as $category){
			$this->data["categories"][] = $this->categoriesModel->getLangRecord($category->id,"en");
		}
	}
	
	
	public function index(){
		$this->home("en");
	}
	
	
	
	public function home($lang="en"){
		
		$this->data["lang"] = $lang;
		
		$this->load->helper("simple_image");
		$SI = new SimpleImage();
		
		$this->load->model("productsModel");
		$products = $this->productsModel->getRecords(array("special"=>"1"),0,10);
		foreach($products as $product){
			if($product->image != "")
				$product->image = $SI->getThumbPathWidthHeight($product->image,320,300);
		}
		$this->data["products"] = $products;
		
		$this->data["content"] = $this->load->view("home",$this->data,TRUE);
		$this->load->view('container',$this->data);
	}

	
	public function aboutus($lang="en"){
		
		$this->data["lang"] = $lang;
		
		$cat = $this->categoriesModel->getRecordByInternalName("aboutus",$lang);
		$pages = $this->pagesModel->getRecords(array("category_id"=>$cat->id,"published"=>"1","language"=>$lang),0,1,"insert_time","desc");
		if(isset($pages[0])){
			$this->data["page"] = $pages[0];
			$this->data["content"] = $this->load->view("about",$this->data,TRUE);
		}else{
			$this->data["content"] = $this->load->view("no_page",$this->data,TRUE);
		}
		
		$this->load->view('container',$this->data);
	}
	
	
	
	public function contactus($lang="en"){
		
		$this->data["lang"] = $lang;
		
		$cat = $this->categoriesModel->getRecordByInternalName("contactus",$lang);
		$pages = $this->pagesModel->getRecords(array("category_id"=>$cat->id,"published"=>"1","language"=>$lang),0,1,"insert_time","desc");
		if(isset($pages[0])){
			$this->data["page"] = $pages[0];
			$this->data["content"] = $this->load->view("contactus",$this->data,TRUE);
		}else{
			$this->data["content"] = $this->load->view("no_page",$this->data,TRUE);
		}
		
		$this->load->view('container',$this->data);
	}
	
	
	public function registerProduct($lang="en"){
		$this->data["lang"] = $lang;
		
		$this->data["content"] = $this->load->view("register_product",$this->data,TRUE);
		$this->load->view('container',$this->data);
	}
	
	
	public function submit($lang="en"){
		
		$name = $this->input->post("name");
		$model = $this->input->post("model");
		$company = $this->input->post("company");
		$tel = $this->input->post("tel");
		$email = $this->input->post("email");
		$desc = $this->input->post("desc");
		
		$errors = array();
		
		if(empty($tel) || $tel == ""){
			$errors[] = "شماره تماس الزامیست.";
		}
		
		if(empty($email) || $email == "E-mail:"){
			$errors[] = "ایمیل آدرس الزامیست";
		}else{
			$this->load->helper("email");
			if(!valid_email($email)){
				$errors[] = "ایمیل آدرس وارد شده معتبر نیست";
			}
		}
		
		if(empty($desc) || $desc == ""){
			$errors[] = "توضیحات الزامیست";
		}
		
		if(isset($errors) && count($errors) > 0){
			$this->data["errors"] = $errors;
			$this->registerProduct($lang);
		}else{
			$this->load->library("email");
			
			$config['protocol'] = 'sendmail';
		
			$config['useragent'] = "CI-CHEMIALAB";
			$config['wordwrap'] = TRUE;
			$config['mailtype'] = "html";
			$config['crlf'] = "\n";
			$config['newline'] = "\r\n";
			$config['priority'] = "2";
			
			$this->email->initialize($config);
	
			$this->email->from('no-reply@chemialab.com',"Chemialab.com");
			
			$this->email->to("babak.taraghi@gmail.com"); //info@chemialab.com
	
			$this->email->subject("Chemialab.com Contact Us Form Submited");
	
			$message = "<div style='font-family: Arial;'>
						<h3>Chemialab.com contact us form has been recently submitted with the following information.</h3>
					<br />
					<table>
						<tr>
							<td width='50'></td>
							<td nowrap='nowrap' width='220'> - Company Name</td>
							<td> : ".$company."</td>
						</tr>
						<tr>
							<td></td>
							<td nowrap='nowrap'> - Phone</td>
							<td> : ".$tel."</td>
						</tr>
						<tr>
							<td></td>
							<td nowrap='nowrap'> - Email</td>
							<td> : ".$email."</td>
						</tr>
						<tr>
							<td></td>
							<td nowrap='nowrap'> - Product Name</td>
							<td> : ".$name."</td>
						</tr>
						<tr>
							<td></td>
							<td nowrap='nowrap'> - Product Model</td>
							<td> : ".$model."</td>
						</tr>
						<tr>
							<td></td>
							<td> - Message</td>
							<td> : ".$desc."</td>
						</tr>
					</table>
					<br /><br />Regards,<br />
					Chemialab.com<br /><br /><br /><br />
				</div>";
		
			$this->email->message($message);
			
			$result = $this->email->send();
			
			if($result){
				$this->data["errors"] = array("با تشکر از شما, اطلاعات با موفقیت ارسال شد ");
			}else{
				$this->data["errors"] = array("لطفآ مجددا تلاش کنید");
			}
			$this->registerProduct($lang);
		}
	}
}







